package com.ecocent.struts2;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.ecocent.common.bean.Pager;
import com.ecocent.common.util.JsonUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import net.sf.json.JSONObject;

public class ApiBaseAction<T>  extends ActionSupport {

	private static final long serialVersionUID = 6718838822334455667L;

	public static final String STATUSCODE = "statuscode";
	public static final String STATUSINFO = "statusinfo";
	public static final int STATUS_SUCCESS = 1;
	public static final int STATUS_ERROR = -1;

	protected Long id;
	protected Long[] ids;
	protected Pager pager;
	protected T data;
	public T getData() {
		return data;
	}
	public void setData(T data) {
		this.data = data;
	}
	public String input() {
		return null;
	}
	// 获取Attribute
	public Object getAttribute(String name) {
		return ServletActionContext.getRequest().getAttribute(name);
	}

	// 设置Attribute
	public void setAttribute(String name, Object value) {
		ServletActionContext.getRequest().setAttribute(name, value);
	}

	// 获取Parameter
	public String getParameter(String name) {
		return getRequest().getParameter(name);
	}

	// 获取Parameter数组
	public String[] getParameterValues(String name) {
		return getRequest().getParameterValues(name);
	}

	// 获取Session
	public Object getSession(String name) {
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		return session.get(name);
	}

	// 获取Session
	public Map<String, Object> getSession() {
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		return session;
	}

	// 设置Session
	public void setSession(String name, Object value) {
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		session.put(name, value);
	}

	// 获取Request
	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	// 获取Response
	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	// 获取Application
	public ServletContext getApplication() {
		return ServletActionContext.getServletContext();
	}


	// AJAX输出文本，返回null
	public String ajaxText(String text) {
		return ajax(text, "text/plain");
	}

	// AJAX输出HTML，返回null
	public String ajaxHtml(String html) {
		return ajax(html, "text/html");
	}

	// AJAX输出XML，返回null
	public String ajaxXml(String xml) {
		return ajax(xml, "text/xml");
	}

	// 根据字符串输出JSON，返回null
	public String ajaxJson(String jsonString) {
		return ajax(jsonString, "text/html");
	}
	
	// 根据Map输出JSON，返回null
	public String ajaxJson(Map<String, String> jsonMap) {
		JSONObject jsonObject = JSONObject.fromObject(jsonMap);
		return ajax(jsonObject.toString(), "text/html");
	}
	

	
	// 输出JSON成功消息，返回null
	public String ajaxJsonSuccessMessage(String message) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put(STATUSCODE, STATUS_SUCCESS);
		jsonMap.put(STATUSINFO, message);
		JSONObject jsonObject = JSONObject.fromObject(jsonMap);
		return ajax(jsonObject.toString(), "text/html");
	}
	// 输出JSON成功消息，返回null
	public String ajaxJsonWarnMessage(int statuscode,String message) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put(STATUSCODE, statuscode);
		jsonMap.put(STATUSINFO, message);
		JSONObject jsonObject = JSONObject.fromObject(jsonMap);
		return ajax(jsonObject.toString(), "text/html");
	}

	public String ajaxJsonSuccessMessage(String message, Map<String, Object> map) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put(STATUSCODE, STATUS_SUCCESS);
		jsonMap.put(STATUSINFO, message);
		jsonMap.putAll(map);
		JSONObject jsonObject = JSONObject.fromObject(jsonMap);
		return ajax(jsonObject.toString(), "text/html");
	}
	public String ajaxNewJsonSuccessMessage(String message, Map<String, Object> map) throws Exception {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put(STATUSCODE, STATUS_SUCCESS);
		jsonMap.put(STATUSINFO, message);
		jsonMap.putAll(map);
		String jsonStr = JsonUtil.getJsonStringFromObject(jsonMap);
		return ajax(jsonStr, "text/html");
	}
	// 输出JSON错误消息，返回null
	public String ajaxJsonSystemError(String message) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put(STATUSCODE, STATUS_ERROR);
		jsonMap.put(STATUSINFO, message);
		JSONObject jsonObject = JSONObject.fromObject(jsonMap);
		return ajax(jsonObject.toString(), "text/html");
	}
	// 输出JSON错误消息，返回null
		public String ajaxJsonServiceError(String message,String statusCode) {
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			jsonMap.put(STATUSCODE, statusCode);
			jsonMap.put(STATUSINFO, message);
			JSONObject jsonObject = JSONObject.fromObject(jsonMap);
			return ajax(jsonObject.toString(), "text/html");
		}

	public  String ajax(String content, String type) {
		this.ajax(content, type, 200);
		return null;
	}

	public  String ajax(String content, String type, int status) {
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType(type + ";charset=UTF-8");
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
			response.setStatus(status);
			response.getWriter().write(content);
			response.getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	// 设置页面不缓存
	public void setResponseNoCache() {
		getResponse().setHeader("progma", "no-cache");
		getResponse().setHeader("Cache-Control", "no-cache");
		getResponse().setHeader("Cache-Control", "no-store");
		getResponse().setDateHeader("Expires", 0);
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long[] getIds() {
		return ids;
	}

	public void setIds(Long[] ids) {
		this.ids = ids;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

}
